package com.wanxi.spring.cloud.security.controller;

import com.wanxi.spring.cloud.commons.api.CommonResult;
import com.wanxi.spring.cloud.security.model.User;
import com.wanxi.spring.cloud.security.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags="安全管理")
@RestController
@RequestMapping("/security")
public class SecurityController {

    @Autowired
    UserService userService;

    @GetMapping("/getUserByName")
    @ApiOperation("根据用户编号查找用户")
    @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "String",dataType = "json")
    public CommonResult getUserByName(@RequestParam String username) {
        return userService.getUserByName(username);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiImplicitParam(name = "user",value = "User实体类",required = true,paramType = "User",dataType = "json")
    public CommonResult login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/sendToken")
    public CommonResult sendToken(String authHeader){
        return userService.sendToken(authHeader);
    }

    @GetMapping("/code")
    public void code(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        userService.code(req,resp);
    }

    @PostMapping("/validateCode")
    public CommonResult validateCode(@RequestParam String code,HttpServletRequest req){
        return userService.validateCode(code,req);
    }
}
